﻿/*将导入到图库的图片素材，按照文件夹规则，分别生成组件，并把对应的动画帧插入到时间轴*/

/*生成组件*/
var doc = fl.getDocumentDOM();

var lib = doc.library;

//lib.selectAll();


var items = lib.getSelectedItems();

if(items.length == 0){
	alert("请选择要操作的文件夹");
}else{
	var tempx=200, tempy=200, maxColumnWidth=0, totalWidth=0, totalHeight; 

	makeSymbol(items);
	doc.save();
}


/*
	新建元件，并填补动画帧
	如果是图片，执行创建元件，，如果是文件夹，将子集全部选中，然后再递归调用此方法
*/
function makeSymbol(picItems){
	
	
	var item = picItems[0];

	if(item.itemType == "bitmap"){
			
			/*for(var i = 0;i < picItems.length;i++){
		
				var item= picItems[i];
				fl.trace(item.name);
			}
			fl.trace("------------");
			return;	*/
			var newName = item.name.substring(0,item.name.length-4);
			lib.addItemToDocument({x:tempx, y:tempy},item.name);
		
			// 转换为元件
			doc.convertToSymbol('movie clip', newName, 'top left');
			lib.selectItem(newName,false);

			if (lib.getItemProperty('linkageImportForRS') == true) 
			{
					lib.setItemProperty('linkageImportForRS', false);
			}
			lib.setItemProperty('linkageExportForAS', true);
			lib.setItemProperty('linkageExportForRS', false);
			lib.setItemProperty('linkageExportInFirstFrame', true);
			var nameArray = newName.split("/");
			lib.setItemProperty('linkageClassName', nameArray[nameArray.length -3] + "_0_" + nameArray[nameArray.length -2]);
			lib.setItemProperty('scalingGrid',  false);
			var nowItemsSelected = lib.getSelectedItems();
			var symbolItem = nowItemsSelected[nowItemsSelected.length -1];
			var timeline = symbolItem.timeline; 
			//timeline.addNewLayer();

			timeline.removeFrames(0, timeline.frameCount)

		
			
			
			doc.enterEditMode("inPlace"); 
			//for(var j=0;j<itemsSelected.length;j++){
			var frameKey = 1;//1拍几，就对应几
			var returnStr = "";
			if(item.name.indexOf("/attack/") > -1){
				frameKey = 1;
			}else if(item.name.indexOf("/death/") > -1){
				frameKey = 2;
			}else if(item.name.indexOf("/stand/") > -1){
				frameKey = 3;
				returnStr = nameArray[nameArray.length -3];
			}
			for(var j=0;j<picItems.length;j++){
				var item = picItems[j];
				
				var multiJ = j * frameKey;
				if(item){
					if(item.name.indexOf("/attack/") > -1){
						
					}
					timeline.setSelectedFrames(multiJ, multiJ);
					timeline.insertBlankKeyframe(multiJ);
					timeline.setSelectedFrames(multiJ, multiJ);
					lib.addItemToDocument({x:0,y:0},item.name)
				}
			}
			timeline.insertFrames(frameKey-1);
			timeline.setSelectedFrames(0,0);//图片插入完成，返回第一帧
			
			doc.exitEditMode();//退出编辑模式
			return returnStr;
			
	}
	
	for(var i = 0;i < picItems.length;i++){
		var item = picItems[i];
		//fl.trace("itemname = " + item.name)
		if(item.itemType == "folder"){//选择下一级目录，并去掉第一个
			lib.selectItem(item.name);
			var subItems = lib.getSelectedItems();
			if(subItems.length >=2){
					var secondItem = subItems[1];
			
					if(secondItem.itemType == "bitmap"){
						var returnStr = makeSymbol(subItems.slice(1,picItems.length -1));
						//如果stand动画完成，表示一个场景完成，可以导出了，导出完之后清空场景。
						if(returnStr != ""){
							
							
							var url = "file:///C|/outputSWF/" + returnStr + "_0.swf"
							fl.getDocumentDOM().exportSWF(url);
							//删除当前所有的movie clip
							
							fl.getDocumentDOM().library.selectAll();
							var allItems  = fl.getDocumentDOM().library.getSelectedItems();
							for(var j = 0;j < allItems.length;j++){
								var toDelItem = allItems[j];
								if(toDelItem.itemType =="movie clip"){
									fl.getDocumentDOM().library.deleteItem(toDelItem.name);
								}
							}
							//fl.getDocumentDOM().selectAll();//选中场景中所有的位图
							//if(fl.getDocumentDOM().selection.length>0)
							//{
							//	fl.getDocumentDOM().deleteSelection();//删除所有位图
							//}
							
						}
					}
			}
			
		}
	}
	
}



//doc.selectAll();
//doc.deleteSelection();